package com.lh.singleton.type7;

//无参构造
public class Singleton1 {
	public static void main(String[] args) {

		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();

		System.out.println(instance.hashCode() == instance2.hashCode());
		instance.show();
	}
}

class Singleton {

	// 1.构造器私有化，外部能new
	private Singleton() {

	}

	// 本类内部创建对象实例
	private static volatile Singleton instance;

	//静态内部类，该类有一个静态属性Singleton
	public static class SingletonInstance {
		private final static Singleton instance = new Singleton();
	}

	//即懒汉式
	public static synchronized Singleton getInstance() {

		return SingletonInstance.instance;
	}

	public void show() {
		System.out.println("测试一下");
	}
}